﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

public partial class Cities : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Location> citiesLocation = new List<Location>{
                                    new Location { City="Los Angeles", Distance=960, Country="USA"},
                                    new Location { City="New York", Distance=2404, Country="USA" },
                                    new Location { City="Beijing", Distance=5405, Country="China" },
                                    new Location { City="Mumbai", Distance=7750, Country="India" },
                                    new Location { City="Tokyo", Distance=4784, Country="Japan" },
                                    new Location { City="Sydney", Distance=7757, Country="Australia" },
                                    new Location { City="Singapore", Distance=8075, Country="Singapore" },
                                    new Location { City="Toronto", Distance=2066, Country="Canada" },
                                    new Location { City="Moscow", Distance=5207, Country="Russia" },
                                    new Location { City="Frankfurt", Distance=5083, Country="Germany" },
                                    new Location { City="Athens", Distance=6171, Country="Greece" }
                                };
        GridView1.DataSource = citiesLocation;
        GridView1.DataBind();

        // TODO Order list by country name

        //GridView2.DataSource = from location in citiesLocation
        //                       where location.Distance > 1000
        //                       orderby location.Country, location.City
        //                       select location;

        //GridView2.DataBind();


        TravelOrganizer travel = new TravelOrganizer();

        GridView3.DataSource = from location in travel.PlacesVisited
                               where location.Distance > 1000
                               orderby location.Country, location.City
                               select location;
        GridView3.DataBind();

        TravelOrganizer travelSkipTake = new TravelOrganizer();
        GridView4.DataSource = (from location in travelSkipTake.PlacesVisited
                                orderby location.Distance descending
                                select location).Skip(1).Take(5);
        GridView4.DataBind();

        // Calculate farthest city away
        Location farthestCity = (from location in travel.PlacesVisited
                                 orderby location.Distance descending
                                 select location).First();

        MaxCityNameTxt.Text = farthestCity.City;
        MaxCityDistanceTxt.Text = "(" + farthestCity.Distance + " miles)";

        // Calculate total city distances of all cities inside US
        int totalDistance = (from location in travel.PlacesVisited
                             where location.Country == "USA"
                             select location).Sum(loc => loc.Distance);
        TotalDistanceTxt.Text = totalDistance + " miles";

        // Calculate average city distances of each city trip
        double averageDistance = travel.PlacesVisited.Average(loc => loc.Distance);
        AverageDistanceTxt.Text = averageDistance + " miles";
    }
}

public class Location
{
    // Fields
    private string _country;
    private int _distance;
    private string _city;

    // Properties
    public string Country
    {
        get { return _country; }
        set { _country = value; }
    }

    public int Distance
    {
        get { return _distance; }
        set { _distance = value; }
    }

    public string City
    {
        get { return _city; }
        set { _city = value; }
    }
}

public class TravelOrganizer
{
    public List<Location> PlacesVisited

    {
        get
        {
            List<Location> cities = new List<Location>{
                                        new Location { City="Los Angeles", Distance=960, Country="USA" },
                                        new Location { City="New York", Distance=2404, Country="USA" },
                                        new Location { City="Beijing", Distance=5405, Country="China" },
                                        new Location { City="Mumbai", Distance=7750, Country="India" },
                                        new Location { City="Tokyo", Distance=4784, Country="Japan" },
                                        new Location { City="Sydney", Distance=7757, Country="Australia" },
                                        new Location { City="Singapore", Distance=8075, Country="Singapore" },
                                        new Location { City="Toronto", Distance=2066, Country="Canada" },
                                        new Location { City="Moscow", Distance=5207, Country="Russia" },
                                        new Location { City="Frankfurt", Distance=5083, Country="Germany" },
                                        new Location { City="Athens", Distance=6171, Country="Greece" }
                                    };
            return cities;
        }
    }
}
